#include <dao/FriendDao.h>
#include <dao/MySQLConnPool.h>
#include <entity/FriendInfoEntity.h>
#include <cppwinks/utility/format/Format.h>
#include <iostream>
#include <cppwinks/utility/json/Json.h>

using namespace std;
using namespace mysql;
using namespace format;
using namespace json;

FriendDao *friend_dao = new FriendDao;

vector<int> FriendDao::getFriendIds(int user_id)
{
    //用于收纳用户好友ID
    vector<int> ret;

    shared_ptr<MysqlPreparedStatement> friendStmt = pool->getConnection()->prepareStatement(
        "SELECT * FROM friends WHERE user_id = ? OR friend_id = ?");
    friendStmt->setInt(1, user_id);
    friendStmt->setInt(2, user_id);
    MysqlResult friendRelations = friendStmt->executeQuery();
    while (friendRelations.next())
    {
        int user_id1 = friendRelations.getInt(1);
        int friend_id = friendRelations.getInt(2);
        int friendId = user_id1 == user_id ? friend_id : user_id;
        ret.push_back(friendId);
    }
    return ret;
}

FriendInfoEntity FriendDao::getFriendInfoByid(int user_id)
{
    shared_ptr<MysqlPreparedStatement> userStmt = pool->getConnection()->prepareStatement(
        "SELECT id, username, avatar_url FROM users WHERE id = ?");
    userStmt->setInt(1, user_id);
    MysqlResult userResult = userStmt->executeQuery();
    userResult.next();

    int id = userResult.getInt(0);

    string name = userResult.getString(1);

    string aut = userResult.getString(2);

    FriendInfoEntity tmp(id, name, aut);
    return tmp;
}

// vector<FriendInfoEntity> FriendDao::getfriends(int user_id)
// {
//     // 第一步：获取与当前用户相关的所有好友关系
//     shared_ptr<MysqlPreparedStatement> friendStmt = pool->getConnection()->prepareStatement(
//         "SELECT * FROM friends WHERE user_id = ? OR friend_id = ?");

//     friendStmt->setInt(1, user_id);
//     friendStmt->setInt(2, user_id);

//     MysqlResult friendRelations = friendStmt->executeQuery();

//     vector<FriendInfoEntity> ret;

//     // 第二步：遍历好友关系，获取好友的详细信息
//     while (friendRelations.next())
//     {
//         // 获取好友的 ID（如果当前用户是 user_id，则好友是 friend_id；反之亦然）
//         int friendId = friendRelations.getInt(1) == user_id ? friendRelations.getInt(2) : friendRelations.getInt(1);
//         cout << '1' << endl;
//         // 查询 users 表，获取好友的详细信息
//         shared_ptr<MysqlPreparedStatement> userStmt = pool->getConnection()->prepareStatement(
//             "SELECT id, username, avatar_url FROM users WHERE id = ?");
//         cout << '2' << endl;

//         userStmt->setInt(1, friendId);
//         MysqlResult userResult = userStmt->executeQuery();
//         cout << '3' << endl;

//         if (userResult.next())
//         {
//             cout << "666" << endl;
//             // 将好友信息添加到返回结果中
//             int id = userResult.getInt(0);
//             cout << "777" << endl;
//             string name = userResult.getString(1);
//             cout << "888" << endl;
//             string aut = userResult.getString(2);
//             cout << "999" << endl;
//             FriendInfoEntity tmp(id, name, aut);
//             cout << "000" << endl;
//             ret.push_back(tmp);
//             cout << "=============================" << endl;
//         }
//         cout << '4' << endl;
//     }
//     cout << '5' << endl;

//     return ret;
// }